<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/22
 * Time: 16:18
 */

namespace Admin\Controller;

use Admin\Controller\BaseController;
use Common\ORG\Util\UploadFile;

class OrderController extends BaseController
{
    /**
     * 构造函数
     * ItemController constructor.
     */
    public function __construct()
    {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/order/quotedOrderItemList',
            '/admin/order/ajaxOrderItemQuoted',
            '/admin/order/ajaxCheckOrderStatus',
            '/admin/order/ajaxUploadQuotedDetail',
            '/admin/order/exportOrderList',
            '/admin/order/ajaxRefuseAcceptOrder',
            '/admin/order/ajaxSuccessAcceptOrder',
            '/admin/order/ajaxResetOrder',
            '/admin/order/ajaxOrderRefuse',
            '/admin/order/ajaxOrderRemark'
        ]);
        parent::__construct();
    }

    //业支订单列表
    public function orderList()
    {
        $company_name = I("get.company_name");
        $department = I("get.department");
        $admin_name = I("get.admin_name");
        $search_time = I("get.search_time");
        $status = I("get.status", 0);
        $start = '';
        $end = '';
        if ($search_time) {
            $all_time = explode('-', $search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1] . " 23:59:59");
        }

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $status && $params['status'] = $status;
        $start && $params['start'] = $start;
        $end && $params['end'] = $end;

        $result = D('Order', 'Logic')->searchOrderList($params);

        //统计
        $statOrder = D('Resource/Order', 'Service')->getStatOrderInfo(['status' => 55]);

        $userList = D('Resource/User', 'Service')->getUserList([]);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/order/orderList?company_name={$company_name}&status={$status}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);
        $this->assign('search_time', $search_time);

        $this->assign('statOrder', $statOrder);
        $this->assign('userList', $userList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('orderList');
    }


    /**
     * 业支订单导出
     */
    public function exportOrderList()
    {
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $admin_id = I("get.admin_id");
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if ($search_time) {
            $all_time = explode('-', $search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1] . " 23:59:59");
        }
        $params = [];
        $admin_id && $params['admin_id'] = $admin_id;
        $start && $params['start'] = $start;
        $end && $params['end'] = $end;

        $result = D('Order', 'Logic')->exportOrderData($params);
        $xls = new \Org\Util\PhpExcel;
        $xls->addArray($result);
        $xls->generateXML("order_" . date("Y-m-d H:i:s"));
    }

    /**
     * 受理下单表
     */
    public function yzWaitAcceptOrderList()
    {
        $user = self::$user;
        $company_name = I("get.company_name");
        $department = I("get.department");
        $admin_name = I("get.admin_name");
        $status = ['in', [45, 50]];

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $status && $params['status'] = $status;

        $result = D('Order', 'Logic')->searchOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/order/yzWaitAcceptOrderList?company_name={$company_name}&status={$status}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('yzWaitAcceptOrderList');
    }

    /**
     * 受理下单表
     */
    public function yzAlreadyAcceptOrderList()
    {
        $user = self::$user;
        $company_name = I("get.company_name");
        $department = I("get.department");
        $admin_name = I("get.admin_name");
        $status = ['in', [50, 55]];

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $status && $params['status'] = $status;

        $result = D('Order', 'Logic')->searchOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/order/yzAlreadyAcceptOrderList?company_name={$company_name}&status={$status}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('yzAlreadyAcceptOrderList');
    }

    /**
     * 业支受理下单
     */
    public function acceptOrder()
    {
        $order_id = I('get.id');
        $orderInfo = D('Order', 'Logic')->getOrderDetail(['id' => $order_id]);

        $this->assign('orderInfo', $orderInfo);
        $this->loadFrame('acceptOrder');

    }

    /**
     * 业支受理下单保存
     */
    public function ajaxSaveOrder()
    {
        $param = I('post.');
        $user = self::$user;
        $order_id = isset($param['id']) ? intval($param['id']) : '';
        $name = isset($param['name']) ? strval($param['name']) : '';
        $agent_name = isset($param['agent_name']) ? strval($param['agent_name']) : '';
        $account_company = isset($param['account_company']) ? strval($param['account_company']) : '';
        $company_name = isset($param['company_name']) ? strval($param['company_name']) : '';
        $ditch_company = isset($param['ditch_company']) ? strval($param['ditch_company']) : '';
        $mobile = isset($param['mobile']) ? strval($param['mobile']) : '';
        $product_name = isset($param['product_name']) ? strval($param['product_name']) : '';
        $amount = isset($param['amount']) ? floatval($param['amount']) : '';
        $pay_interval = isset($param['pay_interval']) ? intval($param['pay_interval']) : '';
        $protocol_number = isset($param['protocol_number']) ? strval($param['protocol_number']) : '';
        $quoted_price = isset($param['quoted_price']) ? floatval($param['quoted_price']) : '';
        $recharge_price = isset($param['recharge_price']) ? floatval($param['recharge_price']) : '';
        $rebate = isset($param['rebate']) ? intval($param['rebate']) : '';
        $remark = isset($param['remark']) ? strval($param['remark']) : '';
        $is_filing = isset($param['is_filing']) ? strval($param['is_filing']) : '';
        $is_bill = isset($param['is_bill']) ? strval($param['is_bill']) : '';
        $is_pay = isset($param['is_pay']) ? strval($param['is_pay']) : '';
        $pay_company = isset($param['pay_company']) ? strval($param['pay_company']) : '';
        $pay_amount = isset($param['pay_amount']) ? floatval($param['pay_amount']) : '';
        $pay_time = isset($param['pay_time']) ? strtotime($param['pay_time']) : '';
        $forecast_time = isset($param['forecast_time']) ? strtotime($param['forecast_time']) : '';
        $user_name = isset($param['user_name']) ? strval($param['user_name']) : '';
        $contract_number = isset($param['contract_number']) ? strval($param['contract_number']) : '';

        if (!$order_id)
            return Response(2001, '数据异常');
        if (!$name)
            return Response(2001, '请填写联系人');
        if (!$company_name)
            return Response(2001, '请填写终端客户名称');
        if (!$agent_name)
            return Response(2001, '请填写公司名（代理商）');
        if (!$ditch_company)
            return Response(2001, '请填写渠道公司名称');
        if (!preg_match("/^1[345678]\d{9}$/", $mobile))
            return Response(2001, '请正确填写手机号');
        if (!$product_name)
            return Response(2001, '请填写项目名');
        if (!$amount)
            return Response(2001, '请填写销售金额');
        if (!$protocol_number)
            return Response(2001, '请填写协议号');
        if (!$quoted_price)
            return Response(2001, '请填写支付金额');
        if ($is_pay == 1 && !$pay_company)
            return Response(2002, '请填写支付方公司名称');
        if ($is_pay == 1 && !$pay_amount)
            return Response(2002, '请填写支付金额');
        if ($is_pay == 1 && !$pay_time)
            return Response(2002, '请填写支付时间');

        $rate = 0;
        if ($rebate > 0) {
            $rate = round($rebate / 100, 2);
        }
        $other_price = round($amount * $rate);
        $profit_price = $amount - $quoted_price - round($amount * $rate);
        if ($profit_price < 0) {
            $profit_price = 0;
        }

        $data = array();
        $data_c = array();
        $data['id'] = $order_id;
        $data['name'] = $name;
        $data['company_name'] = $company_name;
        $data['agent_name'] = $agent_name;
        $data['account_company'] = $account_company;
        $data['ditch_company'] = $ditch_company;
        $data['amount'] = $amount;
        $data['protocol_number'] = $protocol_number;
        $data['quoted_price'] = $quoted_price;
        $data['base_price'] = $quoted_price;
        $data['profit_price'] = $profit_price;
        $data['other_price'] = $other_price;
        $data['recharge_price'] = $recharge_price;
        $data['rebate'] = $rebate;
        $data['remark'] = $remark;
        $data['is_filing'] = $is_filing;
        $data['is_bill'] = $is_bill;
        $data['is_pay'] = $is_pay;
        $data['pay_amount'] = $pay_amount;
        $data['pay_company'] = $pay_company;
        $data['pay_interval'] = $pay_interval;
        $data['pay_time'] = $pay_time;
        $data['forecast_time'] = $forecast_time;
        $data['user_name'] = $user_name;
        $data['contract_number'] = $contract_number;
        $data['update_time'] = time();
        $data['status'] = 55;


        D('Order', 'Logic')->ajaxSaveOrder($data);
    }

    /**
     * 业支订单详情
     */
    public function orderDetail()
    {
        $order_id = I('get.id');
        if (!$order_id) {
            header('Location:/admin/order/orderList');
            exit();
        }
        $data = [];
        $data['id'] = $order_id;
        $orderInfo = D('Order', 'Logic')->getOrderDetail($data);

        $this->assign('order_id', $order_id);
        $this->assign('orderInfo', $orderInfo);
        $this->loadFrame('orderDetail');
    }

    //业务员订单
    public function salesmanOrderList()
    {
        $user = self::$user;
        $company_name = I("get.company_name");
        $order_code = I("get.order_code");
        $search_time = I("get.search_time");
        $status = I("get.status", 0);
        $flag = I("get.flag", 1);
        $start = '';
        $end = '';
        if ($search_time) {
            $all_time = explode('-', $search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1] . " 23:59:59");
        }
        if (!$status && ($flag == 2)) {
            $xstatus = [['lt', 55], ['neq', -1]];
        }
        if (!$status && ($flag == 3)) {
            $xstatus = 55;
        }

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['admin_id'] = $user['id'];
        $order_code && $params['order_code'] = $order_code;
        $company_name && $params['company_name'] = $company_name;
        $status && $params['status'] = $status;
        $start && $params['start'] = $start;
        $end && $params['end'] = $end;
        if (!$status && ($flag == 2)) {
            $params['status'] = [['lt', 55], ['neq', -1]];
        }
        if (!$status && ($flag == 3)) {
            $params['status'] = 55;
        }

        $result = D('Order', 'Logic')->searchOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/order/salesmanOrderList?flag={$flag}&company_name={$company_name}&status={$status}&search_time={$search_time}&order_code={$order_code}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('order_code', $order_code);
        $this->assign('search_time', $search_time);
        $this->assign('flag', $flag);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('salesmanOrderList');
    }

    /**
     * 业务员订单详情
     */
    public function salesmanOrderDetail()
    {
        $user = self::$user;
        $order_id = I("get.order_id");
        if (!$order_id) {
            header('Location:/order/salesmanOrderList');
            exit();
        }
        $data = [];
        $data['id'] = $order_id;
        $data['admin_id'] = $user['id'];
        $orderInfo = D('Order', 'Logic')->getOrderDetail($data);

        $this->assign('order_id', $order_id);
        $this->assign('orderInfo', $orderInfo);
        $this->loadFrame('salesmanOrderDetail');
    }

    /**
     * 发票申请
     */
    public function applyOrderBill()
    {
        $user = self::$user;
        $order_id = I("get.order_id");
        if (!$order_id) {
            header('Location:/order/salesmanOrderList');
            exit();
        }
        $data = [];
        $data['id'] = $order_id;
        $data['admin_id'] = $user['id'];
        $orderInfo = D('Order', 'Logic')->getOrderInfo($data);

        $data_c = [];
        $data_c['order_id'] = $order_id;
        $data_c['admin_id'] = $user['id'];
        $billInfo = D('OrderBill', 'Logic')->getOrderBillInfo($data_c);

        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList', $bill_typeList);
        $this->assign('orderInfo', $orderInfo);
        $this->assign('billInfo', $billInfo);
        $this->loadFrame('applyOrderBill');
    }

    /**
     * 申请发票保存
     */
    public function ajaxSaveApplyBill()
    {
        $user = self::$user;
        $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : '';
        $bill_id = isset($_POST['bill_id']) ? intval($_POST['bill_id']) : '';
        $admin_id = isset($_POST['admin_id']) ? intval($_POST['admin_id']) : '';
        $type = isset($_POST['type']) ? intval($_POST['type']) : '';
        $order_rate = isset($_POST['order_rate']) ? strval($_POST['order_rate']) : '';
        $company_title = isset($_POST['company_title']) ? strval($_POST['company_title']) : '';
        $bill_title = isset($_POST['bill_title']) ? strval($_POST['bill_title']) : '';
        $protocol_number = isset($_POST['protocol_number']) ? strval($_POST['protocol_number']) : '';
        $item_type = isset($_POST['item_type']) ? strval($_POST['item_type']) : '';
        $is_filing = isset($_POST['is_filing']) ? intval($_POST['is_filing']) : '';
        $item_name = isset($_POST['item_name']) ? strval($_POST['item_name']) : '';
        $arrival_time = isset($_POST['arrival_time']) ? $_POST['arrival_time'] : '';
        $amount = isset($_POST['amount']) ? floatval($_POST['amount']) : 0.00;
        $description = isset($_POST['description']) ? strval($_POST['description']) : '';
        $send_type = isset($_POST['send_type']) ? intval($_POST['send_type']) : '';
        $taxpayer_id_number = isset($_POST['taxpayer_id_number']) ? strval($_POST['taxpayer_id_number']) : '';
        $address = isset($_POST['address']) ? strval($_POST['address']) : '';
        $send_address = isset($_POST['send_address']) ? strval($_POST['send_address']) : '';
        $bank = isset($_POST['bank']) ? strval($_POST['bank']) : '';
        $bank_account = isset($_POST['bank_account']) ? strval($_POST['bank_account']) : '';
        $mobile = isset($_POST['mobile']) ? strval($_POST['mobile']) : '';

        if (!$order_id || !$admin_id)
            return Response(2001, "数据异常");
        if ($admin_id != $user['id'])
            return Response(201, "你没有申请的权限");
        if (!$order_rate)
            return Response(2001, "请填写订单占比");
        if (!$company_title)
            return Response(2001, "请填写开票公司抬头");
        if (!$bill_title)
            return Response(2001, "请填写开票抬头");
        if (!$protocol_number)
            return Response(2001, "请填写协议号");
        if (!$item_name)
            return Response(2001, "请填写客户产品名称");
        if (!$item_type)
            return Response(2001, "请填写产品类型");
        if (!$arrival_time)
            return Response(2001, "请填写预计到账时间");
        if (!$description)
            return Response(2001, "请填写开票内容");
        if (!$amount)
            return Response(2001, "请填写开票金额");
        if (!$send_type)
            return Response(2001, "请填写寄送方式");

        if (($type == 2) && !$taxpayer_id_number) return Response(2002, "请填写纳税人识别号");
        if (($type == 2) && !$address) return Response(2002, "请填写地址");
        if (($type == 2) && !$bank) return Response(2002, "请填写开户行");
        if (($type == 2) && !$bank_account) return Response(2002, "请填写开户行账号");
        if (($type == 2) && !$mobile) return Response(2002, "请填写联系电话");

        $data = [];
        $data['order_rate'] = $order_rate;
        $data['order_id'] = $order_id;
        $data['admin_id'] = $admin_id;
        $data['bill_id'] = $bill_id;
        $data['admin_id'] = $admin_id;
        $data['company_title'] = $company_title;
        $data['bill_title'] = $bill_title;
        $data['protocol_number'] = $protocol_number;
        $data['is_filing'] = $is_filing;
        $data['type'] = $type;
        $data['item_name'] = $item_name;
        $data['item_type'] = $item_type;
        $data['arrival_time'] = $arrival_time;
        $data['amount'] = $amount;
        $data['description'] = $description;
        $data['send_type'] = $send_type;
        $taxpayer_id_number && $data['taxpayer_id_number'] = $taxpayer_id_number;
        $address && $data['address'] = $address;
        $send_address && $data['send_address'] = $send_address;
        $bank && $data['bank'] = $bank;
        $bank_account && $data['bank_account'] = $bank_account;
        $mobile && $data['mobile'] = $mobile;

        D('OrderBill', 'Logic')->ajaxSaveApplyBill($data);
    }

    /**
     * 编辑订单项目
     * @return string
     */
    public function ajaxSaveOrderItem()
    {
        $id = I('post.id');
        $order_id = I('post.order_id');
        $parent_cid = I('post.parent_cid');
        $second_cid = I('post.second_cid');
        $three_cid = I('post.three_cid');
        $item_id = I('post.item_id');
        $advertiser = I('post.advertiser');
        $trade = I('post.trade');
        $port = I('post.port');
        $place = I('post.place');
        $put_time = I('post.put_time');
        $post_bar = I('post.post_bar');
        $promote_product = I('post.promote_product');
        $quoted_type = I('post.quoted_type');
        $s_data = I('post.s_data');
        $eid = I('post.eid');

        if (!$order_id) {
            return Response(201, "网络请求出错");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $parent_cid && $data['parent_cid'] = $parent_cid;
        $second_cid && $data['second_cid'] = $second_cid;
        $three_cid && $data['three_cid'] = $three_cid;
        $item_id && $data['item_id'] = $item_id;
        $id && $data['id'] = $id;
        $advertiser && $data['advertiser'] = $advertiser;
        $trade && $data['trade'] = $trade;
        $port && $data['port'] = $port;
        $post_bar && $data['post_bar'] = $post_bar;
        $place && $data['place'] = $place;
        $put_time && $data['put_time'] = $put_time;
        $eid && $data['eid'] = $eid;
        $promote_product && $data['promote_product'] = $promote_product;
        $s_data && $data['s_data'] = json_encode($s_data);
        $quoted_type && $data['quoted_type'] = $quoted_type;

        if (!$data) {
            return Response(202, "无需操作");
        }

        D('Order', 'Logic')->saveOrderItem($data);
    }

    /**
     * 订单项目删除
     * @return string
     */
    public function ajaxDeleteOrderItem()
    {
        $id = I('post.id');
        $order_id = I('post.order_id');

        if (!$id || !$order_id) {
            return Response(201, "网络请求异常");
        }

        $data = [];
        $data['id'] = $id;
        $data['order_id'] = $order_id;

        D('Order', 'Logic')->ajaxDeleteOrderItem($data);
    }

    /**
     * 订单报价管理（业支）
     */
    public function quotedOrderList()
    {
        $company_name = I("get.company_name");
        $department_id = I("get.department_id");
        $admin_name = I("get.admin_name");
        $status = I("get.status", 0);

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $department_id && $params['department_id'] = $department_id;
        $company_name && $params['company_name'] = $company_name;
        $status && $params['status'] = $status;
        $admin_name && $params['admin_name'] = $admin_name;

        $result = D('Order', 'Logic')->searchQuotedOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/order/quotedOrderList?company_name={$company_name}&status={$status}&department_id={$department_id}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');
        //部门
        $departmentList = D('Resource/User', 'Service')->getDepartmentList([]);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department_id', $department_id);
        $this->assign('admin_name', $admin_name);
        $this->assign('departmentList', $departmentList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('quotedOrderList');
    }

    /**
     * 待报价订单管理（业支）
     */
    public function waitQuotedOrderList()
    {
        $company_name = I("get.company_name");
        $department_id = I("get.department_id");
        $admin_name = I("get.admin_name");
        $status = 5;

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $department_id && $params['department_id'] = $department_id;
        $company_name && $params['company_name'] = $company_name;
        $params['status'] = $status;
        $admin_name && $params['admin_name'] = $admin_name;

        $result = D('Order', 'Logic')->searchQuotedOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/order/waitQuotedOrderList?company_name={$company_name}&status={$status}&department_id={$department_id}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');
        //部门
        $departmentList = D('Resource/User', 'Service')->getDepartmentList([]);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department_id', $department_id);
        $this->assign('admin_name', $admin_name);
        $this->assign('departmentList', $departmentList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('waitQuotedOrderList');
    }

    /**
     * 待排期订单管理（业支）
     */
    public function waitPhaseOrderList()
    {
        $company_name = I("get.company_name");
        $department_id = I("get.department_id");
        $admin_name = I("get.admin_name");
        $status = 20;

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $department_id && $params['department_id'] = $department_id;
        $company_name && $params['company_name'] = $company_name;
        $params['status'] = $status;
        $admin_name && $params['admin_name'] = $admin_name;

        $result = D('Order', 'Logic')->searchQuotedOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/order/waitPhaseOrderList?company_name={$company_name}&status={$status}&department_id={$department_id}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');
        //部门
        $departmentList = D('Resource/User', 'Service')->getDepartmentList([]);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department_id', $department_id);
        $this->assign('admin_name', $admin_name);
        $this->assign('departmentList', $departmentList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('waitPhaseOrderList');
    }


    /**
     * 订单项目报价
     */
    public function quotedOrderItemList()
    {
        $order_id = I('get.order_id');
        if (!$order_id) {
            header('Location:/admin/order/quotedOrderList');
            exit();
        }
        $orderInfo = D('Order', 'Logic')->getOrderDetail(['id' => $order_id]);
        $orderItemList = $orderInfo['itemList'];

        $this->assign('orderInfo', $orderInfo);
        $this->assign('orderItemList', $orderItemList);
        $this->loadFrame('quotedOrderItemList');
    }

    /**
     * 报价上传
     * @return string
     * @throws \Exception
     */
    public function ajaxOrderItemQuoted()
    {
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $order_id = I('post.order_id');
        if (!$order_id) {
            return Response(201, "参数异常");
        }
        if (!empty($_FILES['import']['name'])) {
            $upload = new UploadFile();
            $upload->maxSize = 93145728;
            $upload->saveRule = time;
            $upload->allowExts = array('jpeg,JPG,png,xls,xlsx');
            $upload->savePath = C('STATIC_PATH') . '/attachment/file/quoted/';
            if (!$upload->upload()) {
                return Response(2001, $upload->getErrorMsg());
            }
            //导入数据
            $info = $upload->getUploadFileInfo();

            //解析excel文件
            Vendor('Excel.PHPExcel');
            $file_name = $info[0]['savepath'] . $info[0]['savename'];
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $tmp_result = array();
            for ($i = 2; $i <= $highestRow; $i++) {
                //服务项目id
                $tmp_result = [
                    'resource' => $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue(),
                    'platform' => $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue(),
                    'place' => $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue(),
                    'put_target' => $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue(),
                    'pv' => $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue(),
                    'cpm' => $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue(),
                    'price' => $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue(),
                    'put_time' => \PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue()),
                    'put_day' => $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue(),
                    'total_price' => $objPHPExcel->getActiveSheet()->getCell("J" . $i)->getValue()
                ];
                $data = [];
                $data['resource'] = $tmp_result['resource'];
                $data['platform'] = $tmp_result['platform'];
                $data['place'] = $tmp_result['place'];
                $data['put_target'] = $tmp_result['put_target'];

                $quotedInfo = D('Quoted', 'Logic')->getQuotedInfo($data);
                if (!$quotedInfo) {
                    D('Quoted', 'Logic')->addQuoted($tmp_result);
                }

                $data_o = [];
                $data_o['item_name'] = trim($tmp_result['resource']);
                $data_o['port'] = trim($tmp_result['platform']);
                $data_o['place'] = trim($tmp_result['place']);
                $data_o['post_bar'] = trim($tmp_result['put_target']);
                $data_o['order_id'] = $order_id;
                $orderItemInfo = D('Resource/OrderItem', 'Service')->getOrderItemInfo($data_o);
                if ($orderItemInfo) {
                    //$day = intval($tmp_result['put_day']-1);
                    // if($day<0){
                    //    $day =0;
                    // }
                    $arr = [];
                    $arr['order_id'] = $order_id;
                    $arr['id'] = $orderItemInfo['id'];
                    $arr['price'] = $tmp_result['price'];
                    $arr['total_price'] = $tmp_result['total_price'];
                    //$arr['put_time'] = date('m/d/Y',$tmp_result['put_time'])." - ".date('m/d/Y',strtotime("+{$day} day",$tmp_result['put_time']));
                    //$arr['put_day'] = $tmp_result['put_day'];
                    $arr['status'] = 1;
                    D('Resource/OrderItem', 'Service')->updateOrderItemInfo($arr);
                }
                unset($tmp_result);
                unset($data);
                unset($data_o);
            }
            //删除文件
            unlink($file_name);
            return Response(999, '请求成功');
        } else {
            return Response(2002, "请选择要上传的文件");
        }
    }

    /**
     * 报价&排期
     * @return string
     */
    public function ajaxSaveOrderQuoted()
    {
        $order_id = I('post.order_id');
        $type = I('post.type');
        $schedule_remark = I('post.schedule_remark');
        $post_data = I('post.post_data', []);

        if (!$order_id || !$type) {
            return Response(201, '参数异常');
        }
        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(202, '未查询到订单信息');
        }
        $order_price = 0.00;
        if (is_array($post_data) && count($post_data) > 0) {
            foreach ($post_data as $v) {
                $data = [];
                $data['id'] = $v['id'];
                $data['order_id'] = $order_id;
                $data['price'] = $v['price'] ? $v['price'] : 0.00;
                $data['put_day'] = $v['put_day'];
                $data['total_price'] = $v['total_price'];
                $data['phase'] = $v['phase'];
                if (isset($v['phase']) && $v['phase']) {
                    $time = explode('-', $v['phase']);
                    $day = count_days($time[0], $time[1]);
                    $day = intval($day + 1);
                    if ($day < 0) {
                        $day = 0;
                    }
                    $data['put_day'] = $day;
                }
                $data['status'] = 1;
                if ($type == 'phase') {
                    $data['status'] = 5;
                }
                D('Resource/OrderItem', 'Service')->updateOrderItemInfo($data);
                $order_price += $v['total_price'];
            }

            $arr = [];
            $arr['id'] = $order_id;
            $arr['quoted_price'] = $order_price;
            $arr['base_price'] = $order_price;
            $arr['schedule_remark'] = $schedule_remark;
            $arr['pay_amount'] = $order_price;
            $arr['status'] = 15;
            if ($type == 'phase') {
                $arr['status'] = 25;
            }
            if (!$orderInfo['is_pay']) {
                $arr['pay_amount'] = 0;
            }
            $result = D('Resource/Order', 'Service')->updateOrderInfo($arr);
            if (!$result) {
                Response(204, '网络请求失败！');
            }
        }
        return Response(999, '请求成功');
    }

    /**
     * 订单状态检测
     * @return string
     */
    public function ajaxCheckOrderStatus()
    {
        $order_id = I('post.order_id');
        if (!$order_id) {
            return Response(300, "参数异常");
        }

        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(301, "未查询到订单信息");
        }
        if ($orderInfo['status'] == -1) {
            return Response(302, "该订单销售已关闭");
        }
        if ($orderInfo['status'] == -5) {
            return Response(305, "该订单已拒绝受理");
        }
        if (0 < $orderInfo['status'] && $orderInfo['status'] < 45) {
            return Response(304, "请等待审核完成");
        }
        if ($orderInfo['status'] == 50) {
            return Response(999, "请求成功", ['order_id' => $order_id]);

        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['type'] = 'accept';

        D('Order', 'Logic')->ajaxChangeOrderStatus($data);
    }

    /**
     * 订单拒绝受理
     * @return string
     */
    public function ajaxRefuseAcceptOrder()
    {
        $order_id = I('post.order_id');
        $refuse_accept_reason = I('post.refuse_accept_reason');
        if (!$order_id) {
            return Response(300, "参数异常");
        }

        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(301, "未查询到订单信息");
        }
        if ($orderInfo['status'] == -1) {
            return Response(302, "该订单销售已关闭");
        }
        if ($orderInfo['status'] == -5) {
            return Response(304, "该订单已拒绝受理");
        }
        if ($orderInfo['status'] < 45) {
            return Response(305, "请等待审核完成");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['type'] = 'refuse';
        $data['refuse_accept_reason'] = $refuse_accept_reason;

        D('Order', 'Logic')->ajaxChangeOrderStatus($data);
    }

    /**
     * 受理成单（业支）
     * @return string
     */
    public function ajaxSuccessAcceptOrder()
    {
        $order_id = I('post.order_id');
        if (!$order_id) {
            return Response(300, "参数异常");
        }

        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(301, "未查询到订单信息");
        }

        if ($orderInfo['status'] != 50) {
            return Response(305, "订单状态异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['type'] = 'success';

        D('Order', 'Logic')->ajaxChangeOrderStatus($data);
    }

    /**
     * 报价详情上传
     * @return string
     */
    public function ajaxUploadQuotedDetail()
    {
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $order_id = I('post.order_id');
        $orderItem_id = I('post.orderItem_id');
        if (!$order_id || !$orderItem_id) {
            return Response(201, "参数异常");
        }

        $relative_path = $this->getFilePath(C('STATIC_PATH') . '/attachment/file/quoted/');

        if (!empty($_FILES['import']['name'])) {
            $upload = new UploadFile();
            $upload->maxSize = 93145728;
            $upload->saveRule = "quoted_" . $order_id . "_" . $orderItem_id . "_detail_" . time();
            $upload->allowExts = array('jpeg', 'JPG', 'jpg', 'png', 'xls', 'xlsx');
            $upload->savePath = $relative_path;
            if (!$upload->upload()) {
                return Response(2001, $upload->getErrorMsg());
            }
            //导入数据
            $info = $upload->getUploadFileInfo();

            $data = [];
            $data['id'] = $orderItem_id;
            $data['quoted_file'] = '/attachment/file/quoted/' . $info[0]['savename'];
            $res = D('Resource/OrderItem', 'Service')->updateOrderItemInfo($data);
            if (!$res) {
                return Response(2003, '网络请求失败');
            }
            return Response(999, '请求成功');
        } else {
            return Response(2002, "请选择要上传的文件");
        }
    }

    /**
     * 重新下单
     * 初始化订单流程
     * @return string
     */
    public function ajaxResetOrder()
    {
        $order_id = I('post.order_id');
        if (!$order_id) {
            return Response(300, "参数异常");
        }

        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(301, "未查询到订单信息");
        }

        if ($orderInfo['status'] != -5) {
            return Response(305, "订单状态异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['type'] = 'reset';

        D('Order', 'Logic')->ajaxChangeOrderStatus($data);
    }

    /**
     * 驳回报价 & 排期
     * @return string
     */
    public function ajaxOrderRefuse()
    {
        $order_id = I('post.order_id');
        $type = I('post.type');
        $refuse_reason = I('post.refuse_reason');
        if (!$order_id || !$type) {
            return Response(201, '参数异常');
        }
        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(202, '未查询到订单信息');
        }
        $arr = [];
        $arr['id'] = $order_id;
        // 驳回报价
        if ($type == 'quote') {
            $arr['status'] = -10;
            $arr['refuse_quote_reason'] = $refuse_reason;
        }
        // 驳回排期
        if ($type == 'phase') {
            $arr['status'] = -15;
            $arr['refuse_phase_reason'] = $refuse_reason;
        }

        //var_dump($arr);exit;

        $result = D('Resource/Order', 'Service')->updateOrderInfo($arr);
        if (!$result) {
            Response(204, '网络请求失败！');
        }
        return Response(999, '请求成功');
    }

    /**
     * 路径不存在则创建
     * @return string
     */
    private function getFilePath($file_path)
    {
        if (!file_exists($file_path)) {
            mkdir($file_path,0777,true);
        }
        return $file_path;
    }

    /**
     * 报价 & 排期备注
     * @return string
     */
    public function ajaxOrderRemark()
    {
        $order_id = I('post.order_id');
        $type = I('post.type');
        $remark = I('post.remark');
        if (!$order_id || !$type) {
            return Response(201, '参数异常');
        }
        $orderInfo = D('Resource/Order', 'Service')->getOrderInfo(['id' => $order_id]);
        if (!$orderInfo) {
            return Response(202, '未查询到订单信息');
        }
        $arr = [];
        $arr['id'] = $order_id;
        // 驳回报价
        if ($type == 'quote') {
            $arr['quote_remark'] = $remark;
        }
        // 驳回排期
        if ($type == 'phase') {
            $arr['phase_remark'] = $remark;
        }

        //var_dump($arr);exit;

        $result = D('Resource/Order', 'Service')->updateOrderInfo($arr);
        if (!$result) {
            Response(204, '网络请求失败！');
        }
        return Response(999, '请求成功');
    }

}